package com.baiytfp.hf.HFDataExchange.action.service;

import com.baiytfp.hf.HFDataExchange.action.dao.DeptDAOImpl;
import com.baiytfp.hf.HFDataExchange.common.model.table.User_position;
import com.baiytfp.hf.baiytfp.common.db.HFPager;
import com.baiytfp.hf.baiytfp.framework.base.BaseService;
import com.baiytfp.hf.baiytfp.hfutils.HFDBHelper;
import com.baiytfp.hf.baiytfp.hfutils.HFMap;
import com.baiytfp.hf.HFDataExchange.action.dao.RoleDAOImpl;
import com.baiytfp.hf.HFDataExchange.common.model.base.HFSysParams;
import com.baiytfp.hf.HFDataExchange.common.model.base.TableName;
import com.baiytfp.hf.HFDataExchange.common.model.table.Position;
import com.baiytfp.hf.HFDataExchange.common.model.table.Position_type;
import com.baiytfp.hf.HFDataExchange.common.model.table.Role;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Steven-HF on 2017/5/13.
 */

@Service
public class PositionService extends BaseService {
	public PositionService() throws Exception {

	}

	//岗位
	public List<Map> getPosiList(HFPager hfPager)
			throws Exception {

		Map<String, String> map = hfPager.getParams();

		StringBuffer sWhere = new StringBuffer(" where 1 =1 ");
		StringBuffer sql = new StringBuffer("select pt.posi_type_name, p.* from " + TableName.POSITION + " p " +
				" left join " + TableName.POSITION_TYPE + " pt on p.posi_type=pt.id ");

		List<String> params = new ArrayList<String>();

		if (!"".equals(hfPager.getParam("posi_name"))) {
			if (sWhere.length() > 0) {
				sWhere.append(" and ");
			}
			sWhere.append(" posi_name like ?");
			params.add("%" + HFMap.getIstance().getString(map, "posi_name") + "%");
		}

		List<Map> list = HFDBHelper.list_split(sql.toString() + sWhere.toString() + hfPager.getOrderBy(), Map.class, hfPager, params.toArray());

		return list;
	}

	//岗位类别
	public List<Position_type> getTypeList(HFPager hfPager)
			throws Exception {

		Map<String, String> map = hfPager.getParams();

		StringBuffer sWhere = new StringBuffer(" where 1 =1 ");
		StringBuffer sql = new StringBuffer("select * from " + TableName.POSITION_TYPE);

		List<String> params = new ArrayList<String>();

		if (!"".equals(hfPager.getParam("posi_type_name"))) {
			if (sWhere.length() > 0) {
				sWhere.append(" and ");
			}
			sWhere.append(" posi_type_name like ?");
			params.add("%" + HFMap.getIstance().getString(map, "posi_type_name") + "%");
		}

		List<Position_type> list = HFDBHelper.list_split(sql.toString() + sWhere.toString() + hfPager.getOrderBy(), Position_type.class, hfPager, params.toArray());

		return list;
	}

	/**
	 * 获取该岗位的所有用户
	 * @param posi_id
	 * @return
	 * @throws Exception
	 */
	public List<User_position> getHasUserCodeByUserPosi(String posi_id) throws Exception {
		return HFDBHelper.list(TableName.USER_POSITION, " where posi_id=? ", User_position.class, new Object[]{posi_id});
	}
}

